package com.verisette.ncos.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;

public class MailConstant {
	public static Properties SMTP_PROPS = null; 
	
	public static String SMTP_USER = "wirojsin@verisette.com";
	public static String SMTP_PASSWORD = "ncos2012";
	
	public static String MAIL_TEMPLATE_FOLDER = "RESOURCES\\MAIL";
	public static ConcurrentHashMap<String, String> mailTemplateMap = null;
	
	static {
		try{
			ResourceBundle mailResource = ResourceBundle.getBundle("MailConstant");

			SMTP_PROPS = new Properties();
			SMTP_PROPS.put("mail.smtp.auth", mailResource.getString("SMTP_AUTH"));
			SMTP_PROPS.put("mail.smtp.starttls.enable", mailResource.getString("SMTP_TLS"));
			SMTP_PROPS.put("mail.smtp.host", mailResource.getString("SMTP_HOST"));
			SMTP_PROPS.put("mail.smtp.port", mailResource.getString("SMTP_PORT"));
			
			SMTP_USER = mailResource.getString("SMTP_USER");
			SMTP_PASSWORD = mailResource.getString("SMTP_PASSWORD");
			
			MAIL_TEMPLATE_FOLDER=mailResource.getString("MAIL_TEMPLATE_FOLDER");
			
			mailTemplateMap = new ConcurrentHashMap<String, String>();
			File mailTemplateDir = new File( ProjectConstant.BASE_APP_FOLDER + "\\" + MAIL_TEMPLATE_FOLDER);
			if(!mailTemplateDir.exists()){
				mailTemplateDir.mkdirs();
			} else {
				FileInputStream fis = null;
				InputStreamReader isr = null;
				BufferedReader br = null;
				StringBuilder sbd = null;
				for( File f : mailTemplateDir.listFiles()){
					sbd = new StringBuilder();
					try{
						fis = new FileInputStream(f);
						isr = new InputStreamReader(fis, "UTF-8");
						br = new BufferedReader( isr );
						String crLine;
						while((crLine = br.readLine())!=null){
							sbd.append(crLine);
						}
					}catch(Exception ex){
						ex.printStackTrace();
					}finally{
						if(fis!=null) { try{ fis.close(); }catch(Exception ex){} }
						if(isr!=null) { try{ isr.close(); }catch(Exception ex){} }
						if(br!=null) { try{ br.close(); }catch(Exception ex){} }
					}
					
					if(StringUtils.isNotBlank(sbd.toString())){
						mailTemplateMap.put(f.getName().replaceAll("\\.html", ""), sbd.toString());
					}
				}
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
